/**
 * 刷新验证码
 */
function flushImgCode(e) {

    //attr 对属性值进行改变，prop是html固有属性
    $(e).attr("src", "http://localhost:9000/validateCode?date=" + new Date());
}

/**
 * 登录
 */
function login() {

    let code = $("#code").val();
    let password = $("#password").val();
    let validateCode = $("#validateCode").val();

    let data = {
        code: code,
        validateCode: validateCode,
        password: password
    };
    $.ajax({
            url: 'http://localhost:9000/user/login',
            data: JSON.stringify(data),
            type: 'post',
            async: false,
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                console.log(data);
                if (data.code === 200) {
                    localStorage.setItem("tokenHead", data.data.tokenHead);
                    localStorage.setItem("token", data.data.token);
                    localStorage.setItem("loginUserCode", data.data.code);
                    window.location.href = "html/back/manager.html";
                } else if (data.code === 404) {
                    alert("账号或密码错误！");
                } else if (data.code === 505) {
                    alert("账号已冻结");
                } else {
                    alert("验证码错误！");
                }
            }
        }
    );

}

/**
 * 跳转到注册页面
 */
function toRegister() {
    window.open("/register.html", "_self");
}